package com.ruoyi.buss.report.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.buss.qmcy.domain.QmcyCustCkjg;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.report.mapper.ReportCkjgLsbMapper;
import com.ruoyi.buss.report.domain.ReportCkjgLsb;
import com.ruoyi.buss.report.service.IReportCkjgLsbService;

/**
 * 存款结构Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class ReportCkjgLsbServiceImpl implements IReportCkjgLsbService 
{
    @Autowired
    private ReportCkjgLsbMapper reportCkjgLsbMapper;

    /**
     * 查询存款结构
     * 
     * @param sjrq 存款结构ID
     * @return 存款结构
     */
    @Override
    public ReportCkjgLsb selectReportCkjgLsbById(String sjrq)
    {
        return reportCkjgLsbMapper.selectReportCkjgLsbById(sjrq);
    }

    /**
     * 查询存款结构列表
     * 
     * @param reportCkjgLsb 存款结构
     * @return 存款结构
     */
    @Override
    public List<ReportCkjgLsb> selectReportCkjgLsbList(ReportCkjgLsb reportCkjgLsb)
    {
        return reportCkjgLsbMapper.selectReportCkjgLsbList(reportCkjgLsb);
    }

    /**
     * 新增存款结构
     * 
     * @param reportCkjgLsb 存款结构
     * @return 结果
     */
    @Override
    public int insertReportCkjgLsb(ReportCkjgLsb reportCkjgLsb)
    {
        reportCkjgLsb.setCreateTime(DateUtils.getNowDate());
        return reportCkjgLsbMapper.insertReportCkjgLsb(reportCkjgLsb);
    }

    /**
     * 修改存款结构
     * 
     * @param reportCkjgLsb 存款结构
     * @return 结果
     */
    @Override
    public int updateReportCkjgLsb(ReportCkjgLsb reportCkjgLsb)
    {
        return reportCkjgLsbMapper.updateReportCkjgLsb(reportCkjgLsb);
    }

    /**
     * 批量删除存款结构
     * 
     * @param sjrqs 需要删除的存款结构ID
     * @return 结果
     */
    @Override
    public int deleteReportCkjgLsbByIds(String[] sjrqs)
    {
        return reportCkjgLsbMapper.deleteReportCkjgLsbByIds(sjrqs);
    }

    /**
     * 删除存款结构信息
     * 
     * @param sjrq 存款结构ID
     * @return 结果
     */
    @Override
    public int deleteReportCkjgLsbById(String sjrq)
    {
        return reportCkjgLsbMapper.deleteReportCkjgLsbById(sjrq);
    }

    @Override
    public List<ReportCkjgLsb> selectQmcyCustCkjgList(ReportCkjgLsb reportCkjgLsb, LoginUser userInfo) {
        List<ReportCkjgLsb> doList = new ArrayList<ReportCkjgLsb>();

        // 获取上个月最后一天
        reportCkjgLsb
                .setMonthEnd(reportCkjgLsb.getSjrq() == null || reportCkjgLsb.getSjrq().length()<8?
                        DateUtilss.getLastDayOfMonth(Integer.valueOf(DateUtils.getMMddStr().substring(0, 2))-1):
                        Integer.valueOf(reportCkjgLsb.getSjrq().substring(4,6)) == 1 ?
                                Integer.valueOf(reportCkjgLsb.getSjrq().substring(0,4))-1+"1231":reportCkjgLsb.getSjrq().substring(0,4)+
                                DateUtilss.getLastDayOfMonths(Integer.valueOf(reportCkjgLsb.getSjrq().substring(4,6))-1));
        // 获取去年最后一天
        reportCkjgLsb.setYearEnd(reportCkjgLsb.getSjrq() == null || reportCkjgLsb.getSjrq().length()<8? DateUtilss.getLastYear(new Date(), -1):
                Integer.valueOf(reportCkjgLsb.getSjrq().substring(0,4))-1+"1231");

        
        /**
         * 判断数据日期是否为近3年4个季度以及当年每个月月末
         */
        ArrayList<String> sjrqs = DateUtilss.getThreeYearFourQuarterAndMonth();
        if (!StringUtils.isEmpty(reportCkjgLsb.getSjrq())) {
            if ("9999".equals(reportCkjgLsb.getSjrq())) {
                reportCkjgLsb.setSjrq("");
            }
            sjrqs = new ArrayList<String>();
            String sjrq = reportCkjgLsb.getSjrq();
            sjrq = sjrq.replace("-", "");
            sjrqs.add(sjrq);
        }
        reportCkjgLsb.setSjrqs(sjrqs);
        doList = reportCkjgLsbMapper.ckjg(reportCkjgLsb);

        return doList;
    }
}
